package com.hm.common.plugin;

import java.sql.Connection;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.RowBounds;

@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })
public class OraclePaginationPlugin extends PaginationBasePlugin {
	@Override
	protected String paginationSql(String sql, RowBounds rowBounds) {
		sql = sql.trim();
		StringBuffer pagingSelect = new StringBuffer(sql.length() + 100);
		pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
		pagingSelect.append(sql);
		pagingSelect.append(" ) row_ ) where rownum_ > ").append(rowBounds.getOffset()).append(" and rownum_ <= ")
				.append(rowBounds.getOffset() + rowBounds.getLimit());
		sql = pagingSelect.toString();

		return sql;
	}
}
